package com.nl.controller.xuyi.controller;


import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.xuyi.bean.XuYiContentTemplateInfo;
import com.nl.controller.xuyi.service.XuYiContentTemplateService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @desc 短信模板控制层
 * @return null
 * @author zhaohz
 * @date 2020/9/8 17:21
 */
@RestController
@RequestMapping("xuYiContentTmp")
@Slf4j
public class XuYiContentTemplateController {

    @Autowired
    private XuYiContentTemplateService xuYiContentTemplateService;


    @ApiOperation(value = "短信模板", notes = "分页查询短信模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "contentName", value = "短信模板名称", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findContentTemplate(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "contentName", required = false) String contentName,
            @RequestParam Map<String, Object> paramMap
    ) {
        System.out.println("短信名称"+contentName);
        PageInfo<XuYiContentTemplateInfo> pageInfo = xuYiContentTemplateService.findContentTemplate(page, limit, contentName);
        log.info("短信模板查询：{}", paramMap.toString());
        log.debug("短信模板查询统计返回数据：{}", pageInfo.getList().toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @ApiOperation(value = "修改短信模板表", notes = "插入及修改项目模板表")
    @PostMapping("configureContentTemplate")
    public int configureContentTemplate(
            @RequestParam(value = "contentId", required = false) String contentId,
            @RequestParam(value = "contentName", required = false) String contentName,
            @RequestParam(value = "contentContent", required = false) String contentContent,
            @RequestParam(value = "flag", required = false) String flag,
            @RequestParam(value = "contentNameChange", required = false) String contentNameChange,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        /*
        模板状态正常
         */
        String contentState = "0";
        /*
        待审核
         */
        String auditState = "0";
        String userMsisdn = user.getMsisdn();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sdf.format(new Date());
        String modifyTime = sdf.format(new Date());
        log.info(createTime);
        int result = 0;
        result = xuYiContentTemplateService.configureContentTemplate(flag,contentId,userMsisdn, contentName, contentContent, createTime, modifyTime, contentState, auditState,contentNameChange);
        log.info("修改短信模板：{}", paramMap.toString());
        log.debug("短信模板修改返回数据：{}", result);
        return result;
    }


    @ApiOperation(value = "删除短信模板", notes = "短信模板删除")
    @PostMapping("remove")
    @ApiImplicitParam(name = "contentId", value = "短信模板ID", required = true, dataType = "String")
    public int deleteUser(
            @RequestParam(value = "contentId") String contentId,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {

        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String userMsisdn = user.getMsisdn();
        int result= xuYiContentTemplateService.deleteContentTemplate(contentId,userMsisdn);
        log.info("短信删除参数：{}", contentId);
        log.debug("短信模板删除返回数据：{}", result);
        return result;
    }


    @ApiOperation(value = "查询单个短信模板", notes = "查询单个短信模板")
    @ApiImplicitParam(name = "contentId", value = "短信模板编号", required = true, dataType = "String")
    @PostMapping("findByContentId")
    public XuYiContentTemplateInfo findContentTemplateByContentId(
            @RequestParam(value = "contentId", required = true) String contentId
    ) {
        XuYiContentTemplateInfo xuYiContentTemplateInfo = xuYiContentTemplateService.findContentTemplateByContentId(contentId);
        log.info("查询单个短信模板信息：{}", contentId);
        log.debug("查询单个角色修改返回数据：{}", xuYiContentTemplateInfo.toString());
        return xuYiContentTemplateInfo;
    }


    @PostMapping("findContentTempById")
    public Response findContentTempById(
            @RequestParam(value = "contentId", required = false) String contentId,
            @RequestParam Map<String, Object> paramMap
    ) {
        System.out.println("短信编码"+contentId);
        List<XuYiContentTemplateInfo> contentTempList = xuYiContentTemplateService.findContentTempById(contentId);
        log.info("短信模板查询：{}", paramMap.toString());
        log.debug("短信模板查询统计返回数据：{}", contentTempList.toString());
        return ResponseUtil.sucess(contentTempList);
    }

}
